from flask import Flask
from flask_migrate import Migrate
import config
import click
from blueprint import index
import models
from flask_jwt_extended import JWTManager
from ext import db

app = Flask(__name__)
app.json.ensure_ascii = False # 解决中文乱码问题
# 加载配置文件
app.config.from_object(config)
# 把app绑定db
db.init_app(app)
# 绑定app和数据库
migrate = Migrate(app, db)
jwt = JWTManager(app)



app.register_blueprint(index.blueprint)





@app.cli.command('qn-init')
def init():
    db.drop_all()
    db.create_all()
    click.echo('初始化数据库')
    

# 添加管理员账号
@app.cli.command('qn-admin')
def drop():
    user = models.UserModel()
    user.username = 'admin'
    user.password = '123456'
    db.session.add(user)
    db.session.commit()
    click.echo('管理员创建成功,账号为admin,密码为123456')

# 创建角色标识
    
@app.cli.command('qn-role')
def add_role():
    roles = [
        {'name': '普通用户', 'code': 'level1'},
        {'name': '会员用户', 'code': 'level2'},
        {'name': '管理员用户', 'code': 'level3'},
    ]
    role1 = models.RoleModel(name=roles[0]['name'], code=roles[0]['code'])
    role2 = models.RoleModel(name=roles[1]['name'], code=roles[1]['code'])
    role3 = models.RoleModel(name=roles[2]['name'], code=roles[2]['code'])
    db.session.add(role1)
    db.session.add(role2)
    db.session.add(role3)
    db.session.commit()
    
    click.echo('角色创建成功')
# 创建路由与标识
    
@app.cli.command('qn-power')
def add_power():
    permission = {
       'read': '/auth/read',
        'commit': '/auth/commit',
        'write': '/auth/write',
        'admin': '/admin',
    }
    power1 = models.PowerModel(code='read', url=permission['read'])
    power2 = models.PowerModel(code='commit', url=permission['commit'])
    power3 = models.PowerModel(code='write', url=permission['write'])
    power4 = models.PowerModel(code='admin', url=permission['admin'])
    db.session.add(power1)
    db.session.add(power2)
    db.session.add(power3)
    db.session.add(power4)
    db.session.commit()
    
    click.echo('路由创建成功')

    
# 给角色分配权限
    
@app.cli.command('qn-role-permission')
def add_role_permission():
    role1 = models.RoleModel.query.filter(models.RoleModel.name == '普通用户').first()
    role2 = models.RoleModel.query.filter(models.RoleModel.name == '会员用户').first()
    role3 = models.RoleModel.query.filter(models.RoleModel.name == '管理员用户').first()
    power1 = models.PowerModel.query.filter(models.PowerModel.code == 'read').first()
    power2 = models.PowerModel.query.filter(models.PowerModel.code == 'commit').first()
    power3 = models.PowerModel.query.filter(models.PowerModel.code == 'write').first()
    power4 = models.PowerModel.query.filter(models.PowerModel.code == 'admin').first()
   
    # 给会员用户添加写入权限
    role2.power.append(power3)
    # 给普通用户添加读取权限、提交权限
    role1.power.append(power1)
    role1.power.append(power2)
    # 给管理员用户添加admin权限
    role3.power.append(power4)
    
    db.session.commit()
   
    click.echo('权限分配成功')
#给用户添加角色
@app.cli.command('qn-role-user')
def add_role_to_user():
    role1 = models.RoleModel.query.filter(models.RoleModel.name == '普通用户').first()
    role2 = models.RoleModel.query.filter(models.RoleModel.name == '会员用户').first()
    role3 = models.RoleModel.query.filter(models.RoleModel.name == '管理员用户').first()
    user = models.UserModel.query.filter(models.UserModel.username == 'admin').first()
    # 给2362400196用户添加管理员角色
    user.role.append(role3)
    # user.role.append(role2)
    # user.role.append(role3)
    click.echo('角色分配成功')
    
    db.session.commit()
    



if __name__ == '__main__':
    app.run(debug=True)

        
            
            